
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
// 删除文件  ---> 每次打包前先删除dist 文件
// const cleanWebpackPlugin = require('clean-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// 抽离css
const extractTextWebpackPlugin = require('extract-text-webpack-plugin')

const webpack = require('webpack')

module.exports = {
  // entry: path.join(__dirname, './src/main.js'),
  entry: {
    app: path.join(__dirname, './src/main.js'),   // 这是项目的主入口文件
    vendors: ['vue', 'vuex', 'vue-router', 'mint-ui', 'axios', 'vue-preview']
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new htmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
      minify: {
          removeComments: true,  // 去除注释
          collapseWhitespace: true, // 合并空白字符
          removeAttributeQuotes: true // 去除属性的引号
      }
    }),
    new CleanWebpackPlugin(),
    new webpack.optimize.CommonsChunkPlugin({  // 优化
      name: 'vendors', // 这个是 entry中指定的 vendors 名  对应起来
      filename: 'js/vendors.js'   // 将第三方包,抽离出来打包到vendors.js 中
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false // 压缩完毕后 移除 警告代码
      }
    }),
    new extractTextWebpackPlugin('css/style.css')  // 抽离出来打包的文件名
  ],
  module: {
    rules: [
      // { test: /\.css$/, use: ['style-loader', 'css-loader'] },
      {
        test: /\.css$/, use: extractTextWebpackPlugin.extract({
          "fallback": "style-loader",
          use: ["css-loader"],
          publicPath: '../'  // 在每个url路径中添加
        })
      },
      {
        test: /\.scss/, use: extractTextWebpackPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader'],
          publicPath: '../'  // 在每个url路径中添加
        })
      },
      { test: /\.vue$/, use: 'vue-loader' },
      { test: /\.jpg|jpeg|png|gif$/, use: 'url-loader?limit=76&name=image/[hash:8]-[name].[ext]' },
      { test: /\.ttf|woff|woff2|eot|svg$/, use: 'url-loader' },
      { test: /\.js$/, use: 'babel-loader', exclude: /node_modules/ }
    ]
  }
}